/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.hyjiacan.deadqq.client.window;

import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;

import javax.swing.BorderFactory;
import javax.swing.GroupLayout;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextField;
import javax.swing.JTree;
import javax.swing.LayoutStyle;
import javax.swing.WindowConstants;

import org.hyjiacan.deadqq.common.entity.User;
import org.hyjiacan.deadqq.common.util.WindowUtil;


/**
 * 用用户登录后看到的主界面，其中包括登录用户的基本信息以及陌生人（好友）列表
 * 
 * @author hyjiacan
 */
public class MainWindow extends JFrame implements Runnable {
	// 登录的用户

	/**
	 * 
	 */
	private static final long serialVersionUID = -8644453656904013633L;
	private User user;

	/**
	 * Creates new form MainWindow
	 */
	public MainWindow() {
	}

	public MainWindow(User user) {
		this.user = user;

		this.addPropertyChangeListener(new PropertyChangeListener() {

			@Override
			public void propertyChange(PropertyChangeEvent arg0) {
				// TODO Auto-generated method stub
				System.out.println("事件:" + arg0.getPropertyName());
			}

		});
	}

	@Override
	public void run() {
		initComponents();

		// 显示登录用户的一些简单信息
		nickname.setText(user.getNickname());
		avatar.setText(user.getAvatar());
		signature.setText(user.getSignature());
		WindowUtil.center(this);
		this.setVisible(true);
		WindowManager.put(this.getClass().getName(), this);
	}

	/**
	 * This method is called from within the constructor to initialize the form.
	 * WARNING: Do NOT modify this code. The content of this method is always
	 * regenerated by the Form Editor.
	 */
	// <editor-fold defaultstate="collapsed"
	// <editor-fold defaultstate="collapsed"
	// desc="Generated Code">//GEN-BEGIN:initComponents
	private void initComponents() {

		profileArea = new JPanel();
		avatar = new JLabel();
		nickname = new JLabel();
		signature = new JTextField();
		contactsScroller = new JScrollPane();
		contactsList = new JTree();

		setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE);
		java.util.ResourceBundle bundle = java.util.ResourceBundle
				.getBundle("com.hyjiacan.xx.client.i18n.en_US"); // NOI18N
		setTitle(bundle.getString("CHATWINDOWTITLE")); // NOI18N
		setMinimumSize(new java.awt.Dimension(260, 380));
		addWindowListener(new WindowAdapter() {
			public void windowClosing(WindowEvent evt) {
				confirmWindowClosing(evt);
			}
		});

		avatar.setBorder(BorderFactory.createLineBorder(new java.awt.Color(204,
				204, 204)));
		avatar.addMouseListener(new MouseAdapter() {
			public void mouseClicked(MouseEvent evt) {
				avatarMouseClicked(evt);
			}
		});

		signature.setEditable(false);
		signature.setBorder(null);

		GroupLayout profileAreaLayout = new GroupLayout(profileArea);
		profileArea.setLayout(profileAreaLayout);
		profileAreaLayout
				.setHorizontalGroup(profileAreaLayout
						.createParallelGroup(GroupLayout.Alignment.LEADING)
						.addGroup(
								profileAreaLayout
										.createSequentialGroup()
										.addContainerGap()
										.addComponent(avatar,
												GroupLayout.PREFERRED_SIZE, 84,
												GroupLayout.PREFERRED_SIZE)
										.addGap(18, 18, 18)
										.addGroup(
												profileAreaLayout
														.createParallelGroup(
																GroupLayout.Alignment.LEADING)
														.addComponent(
																nickname,
																GroupLayout.DEFAULT_SIZE,
																138,
																Short.MAX_VALUE)
														.addComponent(signature))
										.addContainerGap()));
		profileAreaLayout
				.setVerticalGroup(profileAreaLayout
						.createParallelGroup(GroupLayout.Alignment.LEADING)
						.addGroup(
								profileAreaLayout
										.createSequentialGroup()
										.addContainerGap()
										.addGroup(
												profileAreaLayout
														.createParallelGroup(
																GroupLayout.Alignment.LEADING)
														.addGroup(
																profileAreaLayout
																		.createSequentialGroup()
																		.addComponent(
																				nickname,
																				GroupLayout.PREFERRED_SIZE,
																				24,
																				GroupLayout.PREFERRED_SIZE)
																		.addGap(18,
																				18,
																				18)
																		.addComponent(
																				signature,
																				GroupLayout.PREFERRED_SIZE,
																				GroupLayout.DEFAULT_SIZE,
																				GroupLayout.PREFERRED_SIZE))
														.addComponent(
																avatar,
																GroupLayout.PREFERRED_SIZE,
																80,
																GroupLayout.PREFERRED_SIZE))
										.addContainerGap()));

		contactsList.setDragEnabled(true);
		contactsList.setRootVisible(false);
		contactsScroller.setViewportView(contactsList);

		GroupLayout layout = new GroupLayout(getContentPane());
		getContentPane().setLayout(layout);
		layout.setHorizontalGroup(layout
				.createParallelGroup(GroupLayout.Alignment.LEADING)
				.addComponent(profileArea, GroupLayout.DEFAULT_SIZE,
						GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
				.addGroup(
						layout.createSequentialGroup().addContainerGap()
								.addComponent(contactsScroller)
								.addContainerGap()));
		layout.setVerticalGroup(layout.createParallelGroup(
				GroupLayout.Alignment.LEADING)
				.addGroup(
						layout.createSequentialGroup()
								.addComponent(profileArea,
										GroupLayout.PREFERRED_SIZE,
										GroupLayout.DEFAULT_SIZE,
										GroupLayout.PREFERRED_SIZE)
								.addPreferredGap(
										LayoutStyle.ComponentPlacement.RELATED)
								.addComponent(contactsScroller,
										GroupLayout.DEFAULT_SIZE, 481,
										Short.MAX_VALUE).addContainerGap()));

		pack();
	}// </editor-fold>//GEN-END:initComponents

	private void confirmWindowClosing(WindowEvent evt) {// GEN-FIRST:event_confirmWindowClosing
		CloseConfirmDialog.showDialog();
	}// GEN-LAST:event_confirmWindowClosing

	/**
	 * 点击头像时，将显示自己的个人详细信息
	 * 
	 * @param evt
	 */
	private void avatarMouseClicked(MouseEvent evt) {// GEN-FIRST:event_avatarMouseClicked
		if (WindowManager.exists(ProfileView.class.getName() + "-"
				+ user.getNumber())) {
			WindowManager.get(
					ProfileView.class.getName() + "-" + user.getNumber())
					.setVisible(true);
		} else {
			new Thread(new ProfileView(user)).start();
		}
	}// GEN-LAST:event_avatarMouseClicked
		// Variables declaration - do not modify//GEN-BEGIN:variables

	private JLabel avatar;
	private JTree contactsList;
	private JScrollPane contactsScroller;
	private JLabel nickname;
	private JPanel profileArea;
	private JTextField signature;
	// End of variables declaration//GEN-END:variables
}
